<template>
  <el-dialog :model-value="visible" title="新建疗程">
      <el-form>
          <el-form-item label="名称" :label-width="label_width">
              <el-input v-model="treat_name" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="病人邮箱" :label-width="label_width">
              <el-input v-model="patient_email" autocomplete="off"/>
          </el-form-item>
          <el-form-item>
              <el-button
                  style="margin: auto"
                  type="primary"
                  @click="create"
              >
                  新建
              </el-button>
          </el-form-item>
      </el-form>
  </el-dialog>
</template>

<script>

import axios from "axios";
import {ElMessage} from "element-plus";

export default {
    name: "NewTreatPage",
    props: ['visible'],
    data() {
        return {
            label_width: '80px',
            treat_name: '',
            patient_email: ''
        }
    },
    methods: {
        create: function () {
            let newTreatForm = {
                name: this.treat_name,
                user_email: this.patient_email,
                dt_email: this.$store.state.username
            }
            const _this = this;
            axios.post(process.env.VUE_APP_BASEURL + "treatment/create", newTreatForm).then(function (resp) {
                if (resp.data.code === 0) {
                    _this.createSuccess(resp.data.msg)
                } else {
                    _this.createFailure(resp.data.msg)
                }
            })
        },
        createSuccess: function (message) {
            this.$emit('create_success')
            ElMessage({
                message: message,
                type: "success"
            })
        },
        createFailure: function (message) {
            ElMessage({
                message: message,
                type: "error"
            })
        }
    }
}

</script>

<style scoped>

</style>